package com.mall.service.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.*;
import com.mall.model.vo.OmsOrderVo;
import com.mall.service.order.mapper.OmsOrderMapper;
import com.mall.service.order.service.OmsOrderItemService;
import com.mall.service.order.service.OmsOrderOperateHistoryService;
import com.mall.service.order.service.OmsOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements OmsOrderService {

    @Autowired
    OmsOrderOperateHistoryService omsOrderOperateHistoryService;

    @Autowired
    OmsOrderItemService omsOrderItemService;

    @Autowired
    OmsOrderService omsOrderService;


    /**
     * 获取订单详情：订单信息、商品信息、操作记录
     * @param orderId
     * @return
     */
    @Override
    public OmsOrderVo getByIdWithInfo(Long orderId) {
        OmsOrder order = this.getById(orderId);
        OmsOrderVo omsOrderVo = new OmsOrderVo();
        BeanUtils.copyProperties(order,omsOrderVo);
        LambdaQueryWrapper<OmsOrderOperateHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrderOperateHistory::getOrderId,orderId);
        List<OmsOrderOperateHistory> histories = omsOrderOperateHistoryService.list(queryWrapper);
        omsOrderVo.setHistoryList(histories);
        LambdaQueryWrapper<OmsOrderItem> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(OmsOrderItem::getOrderId,orderId);
        List<OmsOrderItem> orderItems = omsOrderItemService.list(queryWrapper1);
        omsOrderVo.setOrderItemList(orderItems);
        return omsOrderVo;
    }

    /**
     * 批量关闭订单
     * @param ids
     * @param note
     */
    @Override
    public void updateWithStatusList(List<Long> ids, String note) {
        for (Long id : ids) {
            OmsOrder order = omsOrderService.getById(id);
            if (order != null && note != null){
                order.setStatus(4);
                order.setNote(note);
                omsOrderService.updateById(order);
            }
        }
    }

    /**
     * 批量发货
     * @param deliveryParamList
     */
    @Override
    public void updateWithDelivery(List<DeliveryParamList> deliveryParamList) {
        for (int i = 0; i < deliveryParamList.size(); i++) {
            DeliveryParamList deliveryParamList1 = deliveryParamList.get(i);
            Long orderId = deliveryParamList1.getOrderId();
            String deliverySn = deliveryParamList1.getDeliverySn();
            String deliveryCompany = deliveryParamList1.getDeliveryCompany();
            OmsOrder order = omsOrderService.getById(orderId);
            order.setDeliverySn(deliverySn);
            order.setDeliveryCompany(deliveryCompany);
            order.setStatus(2);
            omsOrderService.updateById(order);
        }
    }

    /**
     * 修改订单费用信息
     * @param moneyInfoParams
     */
    @Override
    public void updateWithMoneyInfo(OmsMoneyInfoParam moneyInfoParams) {
        Long orderId1 = moneyInfoParams.getOrderId();
        OmsOrder byId = omsOrderService.getById(orderId1);
        byId.setStatus(moneyInfoParams.getStatus());
        byId.setFreightAmount((moneyInfoParams.getFreightAmount()));
        byId.setDiscountAmount(moneyInfoParams.getDiscountAmount());
        omsOrderService.updateById(byId);
        }


    /**
     * 修改收货人信息
     * @param omsReceiverInfoParams
     */
    @Override
    public void updateWithReceiverInfo(OmsReceiverInfoParam omsReceiverInfoParams) {
        Long orderId = omsReceiverInfoParams.getOrderId();
        OmsOrder order = omsOrderService.getById(orderId);
            order.setReceiverCity(omsReceiverInfoParams.getReceiverCity());
            order.setReceiverName(omsReceiverInfoParams.getReceiverName());
            order.setReceiverDetailAddress(omsReceiverInfoParams.getReceiverDetailAddress());
            order.setReceiverPhone(omsReceiverInfoParams.getReceiverPhone());
            order.setReceiverPostCode(omsReceiverInfoParams.getReceiverPostCode());
            order.setReceiverProvince(omsReceiverInfoParams.getReceiverProvince());
            order.setReceiverRegion(omsReceiverInfoParams.getReceiverRegion());
            order.setStatus(omsReceiverInfoParams.getStatus());
            omsOrderService.updateById(order);
        }
    }

